$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/goodshelves/list',
        datatype: "json",
        colModel: [
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
			{ label: '货架名称', name: 'shelvesname', index: 'shelvesName', width: 70 },
			{ label: '货架地址', name: 'shelvesaddress', index: 'shelvesAddress', width: 150 },
			{ label: '货架类型', name: 'type', index: 'type', width: 50,formatter: function(item, index) {
                    if (item === 0) {
                        return '开放式货架';
                    }
                    if (item === 1) {
                        return '带锁货架';
                    }
                } },
            { label: '所属渠道', name: 'franchiseeid', index: 'franchiseeId', width: 50,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.franchiseeid!=null&&rowdata.franchiseeid!=""){
                        var username = "";

                        $.ajax({
                            type: "POST",
                            async:false,
                            url: baseURL + "sys/user/info/"+rowdata.franchiseeid,
                            success: function(r){
                                username =r.user.username;
                            }
                        });

                        return username;
                    }else{
                        return '';
                    }
                } },
			{ label: '货架分组', name: 'shelvesgroup', index: 'shelvesGroup', width: 80,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.shelvesgroup!=null&&rowdata.shelvesgroup!=""){
                    	var groupname = "";
                        var arr = rowdata.shelvesgroup.split(",");
                        for(var i=0;i<arr.length;i++){
                            $.ajax({
                                type: "POST",
                                async:false,
                                url: baseURL + "sys/shelvesgroup/info/"+parseInt(arr[i]),
                                success: function(r){
                                    groupname +=r.shelvesgroup.groupname;
                                    if(i !=arr.length-1){
                                        groupname +="/";
									}

                                }
                            });
						}
						return groupname;
                    }else{
                        return '未分组';
                    }


                } },
			{ label: '状态', name: 'statu', index: 'statu', width: 50,formatter: function(item, index) {
                    if (item === 0) {
                        return '启用';
                    }
                    if (item === 1) {
                        return '禁用';
                    }
                } },
			{ label: '创建时间', name: 'createtime', index: 'createtime', width: 80 },
            { label: '操作', name: 'sy', width: 70,align:'center',formatter: function(cellvalue, options, rowdata) {
                    return "<a style='cursor:pointer;' onclick='vm.update("+ rowdata.id +")'>" +
                        "编辑</a>&nbsp;&nbsp;" +
                        "<a style='cursor:pointer;' onclick='vm.getCode()'>" +
                        "二维码</a>&nbsp;&nbsp;" +
                        "<a style='cursor:pointer;' onclick='vm.del("+ rowdata.id +")'>" +
                        "删除</a>";
                }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    vm.queryGroup();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			name:null,
			type:null,
			statu:null,
            group:null
		},
        groupList: [],
		showList: true,
		title: null,
		goodshelves: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.goodshelves = {};
			vm.getFranchisee();
			vm.getGroup();
            vm.goodshelves.type = 0;
            vm.goodshelves.locktype = 0;
            vm.goodshelves.statu = 0;

		},
        update: function (id) {

            vm.showList = false;
            vm.title = "修改";
            vm.groupList = [];

            vm.getInfo(id);

            vm.getType();
            vm.getGroup();

            if(vm.goodshelves.shelvesgroup!=null&&vm.goodshelves.shelvesgroup!=""){
                var arr = vm.goodshelves.shelvesgroup.split(",");
                for(var i=0;i<arr.length;i++){
                    vm.groupList.push(parseInt(arr[i]));
                    $("#fz"+parseInt(arr[i])).css("border","1px solid #1e8feb").css("color","#1e8feb").css("background-color","#e8f4fd");
                }
            }
        },
		getType: function(){

			if(vm.goodshelves.type == 0){
                $("#lockpassword").hide();
                $("#locktype").hide();
			}
            if(vm.goodshelves.type == 1){
				$("#lockpassword").show();
                $("#locktype").show();
            }
        },
        getCode: function () {
            $('#myModal').modal();
        },
        checkGroup: function(id){
			var ys = $("#fz"+id).attr("style");
			if(ys !="" && typeof ys!="undefined"){

                $("#fz"+id).attr("style","");
                var groupList1 = [];
                if(vm.groupList.length>0){
                    for(var i=0;i<vm.groupList.length;i++){
                    	if(vm.groupList[i]!=id){
                            groupList1.push(vm.groupList[i]);
						}
                    }
                }
                vm.groupList = groupList1;
			}else{
				vm.groupList.push(id);
                $("#fz"+id).css("border","1px solid #1e8feb").css("color","#1e8feb").css("background-color","#e8f4fd");
			}

        },
		saveOrUpdate: function (event) {
			var url = vm.goodshelves.id == null ? "sys/goodshelves/save" : "sys/goodshelves/update";
            vm.goodshelves.shelvesgroup = "";
            if(vm.groupList.length>0){
                for(var i=0;i<vm.groupList.length;i++){
                    vm.goodshelves.shelvesgroup +=vm.groupList[i];
                    if(i!=vm.groupList.length-1){
                        vm.goodshelves.shelvesgroup +=",";
                    }
                }
            }
            if(vm.goodshelves.shelvesname == '' || typeof vm.goodshelves.shelvesname=='undefined'){
                alert("货架名称不能为空");
            }else if(vm.goodshelves.shelvesaddress == '' || typeof vm.goodshelves.shelvesaddress=='undefined'){
                alert("货架地址不能为空");
            }else if(vm.goodshelves.franchiseeid == '' || typeof vm.goodshelves.franchiseeid=='undefined'){
                alert("绑定渠道不能为空");
            }else if(vm.goodshelves.type == 1 &&(vm.goodshelves.lockpassword == '' || typeof vm.goodshelves.lockpassword=='undefined')){
                alert("蓝牙锁密码不能为空");
            }else if(vm.goodshelves.contacts == '' || typeof vm.goodshelves.contacts=='undefined'){
                alert("联系人不能为空");
            }else if(vm.goodshelves.telphone == '' || typeof vm.goodshelves.telphone=='undefined'){
                alert("联系电话不能为空");
            }else{
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.goodshelves),
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(index){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            }

		},
		del: function (id) {

			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/goodshelves/delete/"+id,
                    contentType: "application/json",
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
        	$.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/goodshelves/info/"+id,
                success: function(r){
                    vm.goodshelves = r.goodshelves;

                }
            });

		},
        getGroup: function(){
            $("#group").html("");
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/shelvesgroup/alllist/",
                success: function(r){
                    var temp = "";
                    if(r.alllist!=null&&r.alllist.length>0){
                        $.each(r.alllist, function (i) {

                            temp +="<a class='btn btn-default' onclick='vm.checkGroup("+r.alllist[i].id+")' " +
                                "id='fz"+r.alllist[i].id+"'>"+r.alllist[i].groupname+"</a>&nbsp;&nbsp;";

                        });
                    }else{
                        temp = "您暂未对货架进行分组";
                    }

                    $("#group").append(temp);

                }
            });

        },
        getFranchisee: function(){
            $("#franchisee").html("");
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/goodshelves/infoFranchisee",
                success: function(r){
                    var temp = "";
                    if(r.franchisee!=null&&r.franchisee.length>0){
                        $.each(r.franchisee, function (i) {

                            temp +="<option value='"+r.franchisee[i].userId+"'>"+r.franchisee[i].username+"</option>";

                        });
                    }

                    $("#franchisee").append(temp);

                }
            });

        },
        queryGroup: function(){
            $("#group_id").html("");
            $.get(baseURL + "sys/shelvesgroup/alllist", function(r){
                var temp = "<option value=''></option><option value=''>未分组</option>";
                $.each(r.alllist, function (i) {

                    temp +="<option value='"+r.alllist[i].id+"'>"+r.alllist[i].groupname+"</option>";

                });
                $("#group_id").append(temp);

            });
        },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'name': vm.q.name,"type": vm.q.type,"statu" :vm.q.statu,"group" :vm.q.group},
                page:page
            }).trigger("reloadGrid");
		}
	}
});